# apps/core/tests.py
from django.urls import reverse
from rest_framework.test import APITestCase
from rest_framework import status
from apps.questions.models import Question
from apps.users.models import QBUser
from apps.subjects.models import Subject  # 假设模型在 apps.subjects.models

class CoreTestCase(APITestCase):
    def setUp(self):
        # ✅ 直接用 ORM 创建用户
        self.QBuser = QBUser.objects.create_user(
            username='teacher1',
            password='pass123',
            role='teacher'
        )

        # ✅ 直接用 ORM 创建学科
        self.subject = Subject.objects.create(
            name='测试学科',
            code='TEST',
            level='1'
        )

        # ✅ 现在可以安全创建题目
        self.question = Question.objects.create(
            title='Q1',
            question_type='single_choice',
            difficulty=1,
            creator=self.QBuser,
            subject=self.subject  # ← 传入 Subject 实例
        )

    def test_dashboard_stats(self):
        """测试仪表盘统计"""
        url = reverse('dashboard')
        self.client.force_authenticate(user=self.QBuser)  # 登录用户
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('total_questions', response.data)
        self.assertIn('total_users', response.data)
        self.assertGreaterEqual(response.data['total_questions'], 1)